export function upload() {
  return new Promise<File | undefined>((resolve) => {
    const input = document.createElement('input');
    input.type = 'file';
    input.accept = 'text/csv';
    input.addEventListener('change', (e) => {
      resolve(input?.files?.[0]);
    });
    input.click();
  });
}

export function getCsvSheet() {
  return new Promise<string[][]>(async (resolve) => {
    const errorTemplate = [['读取失败'], ['🍺']];
    const file = await upload();
    if (!file) {
      return errorTemplate;
    }
    const fileReader = new FileReader();
    fileReader.readAsText(file);
    fileReader.onload = () => {
      console.log(fileReader.result);
      resolve(
        fileReader.result
          ?.toString()
          .replace(/\r/g, '')
          .trim()
          .split('\n')
          .map((item) => item.split(';')) ?? errorTemplate
      );
    };
  });
}
